package com.wanho.qz103.hrsaas.sys.service.impl;

import com.wanho.qz103.hrsaas.commons.annontations.Autowired;
import com.wanho.qz103.hrsaas.commons.annontations.Service;
import com.wanho.qz103.hrsaas.commons.annontations.Transactional;
import com.wanho.qz103.hrsaas.commons.exception.DAOException;
import com.wanho.qz103.hrsaas.commons.exception.ServiceException;
import com.wanho.qz103.hrsaas.commons.query.Page;
import com.wanho.qz103.hrsaas.commons.vo.TableVO;
import com.wanho.qz103.hrsaas.sys.dao.PermissionDAO;
import com.wanho.qz103.hrsaas.sys.dao.RoleDAO;
import com.wanho.qz103.hrsaas.sys.pojos.Permission;
import com.wanho.qz103.hrsaas.sys.pojos.Role;
import com.wanho.qz103.hrsaas.sys.service.RoleService;
import com.wanho.qz103.hrsaas.sys.vo.RoleVO;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务实现类
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    /**依赖工厂进行自动赋值*/
    @Autowired
    private RoleDAO roleDAO ;
    @Autowired
    private PermissionDAO permissionDAO ;
    @Override
    public RoleVO findById(String id) throws ServiceException {
        try {
            //1. 查询角色单表数据
            Role role = roleDAO.selectById(id);
            //2.根据角色id 查询权限集合 子查询
            List<Permission> permissionList = permissionDAO.selectByRoleId(id) ;
            List<String> permIds = permissionList.stream().map(Permission::getId).collect(Collectors.toList());
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(roleVO,role);
            roleVO.setPermIds(permIds);
            return roleVO;
        } catch (InvocationTargetException | DAOException | IllegalAccessException e) {
            e.printStackTrace();
            throw new ServiceException("根据角色id查询异常！！！") ;
        }
    }


    @Override
    public boolean assignPrem(Role role) throws ServiceException {
        try {
            //1.删除关系表数据
            boolean delRs = roleDAO.deleteRolePermissionRef(role.getId());
            //2.批量信息关系表
            boolean insertBatch = roleDAO.insertBatchRolePermissionRef(role);
            return delRs&&insertBatch;
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServiceException("分配权限异常！！！") ;
        }
    }

    @Override
    public TableVO findPage(Page page) throws ServiceException {
        try {
            TableVO tableVO = new TableVO();
            long count = roleDAO.selectCount();
            //优化SQL 查询，如果没有数据 无需进行查询当前页数据
            if (count!=0){
                page.setOffset((page.getPage()-1)*page.getSize());
                List<Role> roleList = roleDAO.selectTopN(page);
                tableVO.setRows(roleList);
            }
            tableVO.setTotal(count);
            return tableVO;
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServiceException("查询角色分页列表数据异常！！！") ;
        }
    }

    @Override
    public boolean remove(String id) throws ServiceException {
        try {
            return roleDAO.delete(id);
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServiceException("删除角色业务异常！！！") ;
        }
    }
}
